3 * Implements Special:Watchlist
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup SpecialPage
24 use MediaWiki\MediaWikiServices
;
25 use Wikimedia\Rdbms\ResultWrapper
;
26 use Wikimedia\Rdbms\IDatabase
;
29 * A special page that lists last changes made to the wiki,
30 * limited to user-defined list of titles.
32 * @ingroup SpecialPage
34 class SpecialWatchlist
extends ChangesListSpecialPage
{
35 public function __construct( $page = 'Watchlist', $restriction = 'viewmywatchlist' ) {
36 parent
::__construct( $page, $restriction );
39 public function doesWrites() {
44 * Main execution point
46 * @param string $subpage
48 function execute( $subpage ) {
49 // Anons don't get a watchlist
50 $this->requireLogin( 'watchlistanontext' );
52 $output = $this->getOutput();
53 $request = $this->getRequest();
54 $this->addHelpLink( 'Help:Watching pages' );
55 $output->addModules( [
56 'mediawiki.special.changeslist.visitedstatus',
57 'mediawiki.special.watchlist',
60 $mode = SpecialEditWatchlist
::getMode( $request, $subpage );
61 if ( $mode !== false ) {
62 if ( $mode === SpecialEditWatchlist
::EDIT_RAW
) {
63 $title = SpecialPage
::getTitleFor( 'EditWatchlist', 'raw' );
64 } elseif ( $mode === SpecialEditWatchlist
::EDIT_CLEAR
) {
65 $title = SpecialPage
::getTitleFor( 'EditWatchlist', 'clear' );
67 $title = SpecialPage
::getTitleFor( 'EditWatchlist' );
70 $output->redirect( $title->getLocalURL() );
75 $this->checkPermissions();
77 $user = $this->getUser();
78 $opts = $this->getOptions();
80 $config = $this->getConfig();
81 if ( ( $config->get( 'EnotifWatchlist' ) ||
$config->get( 'ShowUpdatedMarker' ) )
82 && $request->getVal( 'reset' )
83 && $request->wasPosted()
84 && $user->matchEditToken( $request->getVal( 'token' ) )
86 $user->clearAllNotifications();
87 $output->redirect( $this->getPageTitle()->getFullURL( $opts->getChangedValues() ) );
92 parent
::execute( $subpage );
96 * Return an array of subpages that this special page will accept.
98 * @see also SpecialEditWatchlist::getSubpagesForPrefixSearch
99 * @return string[] subpages
101 public function getSubpagesForPrefixSearch() {
112 protected function transformFilterDefinition( array $filterDefinition ) {
113 if ( isset( $filterDefinition['showHideSuffix'] ) ) {
114 $filterDefinition['showHide'] = 'wl' . $filterDefinition['showHideSuffix'];
117 return $filterDefinition;
123 protected function registerFilters() {
124 parent
::registerFilters();
126 $user = $this->getUser();
128 $significance = $this->getFilterGroup( 'significance' );
129 $hideMinor = $significance->getFilter( 'hideminor' );
130 $hideMinor->setDefault( $user->getBoolOption( 'watchlisthideminor' ) );
132 $automated = $this->getFilterGroup( 'automated' );
133 $hideBots = $automated->getFilter( 'hidebots' );
134 $hideBots->setDefault( $user->getBoolOption( 'watchlisthidebots' ) );
136 $registration = $this->getFilterGroup( 'registration' );
137 $hideAnons = $registration->getFilter( 'hideanons' );
138 $hideAnons->setDefault( $user->getBoolOption( 'watchlisthideanons' ) );
139 $hideLiu = $registration->getFilter( 'hideliu' );
140 $hideLiu->setDefault( $user->getBoolOption( 'watchlisthideliu' ) );
142 $reviewStatus = $this->getFilterGroup( 'reviewStatus' );
143 if ( $reviewStatus !== null ) {
144 // Conditional on feature being available and rights
145 $hidePatrolled = $reviewStatus->getFilter( 'hidepatrolled' );
146 $hidePatrolled->setDefault( $user->getBoolOption( 'watchlisthidepatrolled' ) );
149 $authorship = $this->getFilterGroup( 'authorship' );
150 $hideMyself = $authorship->getFilter( 'hidemyself' );
151 $hideMyself->setDefault( $user->getBoolOption( 'watchlisthideown' ) );
153 $changeType = $this->getFilterGroup( 'changeType' );
154 $hideCategorization = $changeType->getFilter( 'hidecategorization' );
155 if ( $hideCategorization !== null ) {
156 // Conditional on feature being available
157 $hideCategorization->setDefault( $user->getBoolOption( 'watchlisthidecategorization' ) );
162 * Get a FormOptions object containing the default options
164 * @return FormOptions
166 public function getDefaultOptions() {
167 $opts = parent
::getDefaultOptions();
168 $user = $this->getUser();
170 $opts->add( 'days', $user->getOption( 'watchlistdays' ), FormOptions
::FLOAT );
171 $opts->add( 'extended', $user->getBoolOption( 'extendwatchlist' ) );
177 * Get all custom filters
179 * @return array Map of filter URL param names to properties (msg/default)
181 protected function getCustomFilters() {
182 if ( $this->customFilters
=== null ) {
183 $this->customFilters
= parent
::getCustomFilters();
184 Hooks
::run( 'SpecialWatchlistFilters', [ $this, &$this->customFilters
], '1.23' );
187 return $this->customFilters
;
191 * Fetch values for a FormOptions object from the WebRequest associated with this instance.
193 * Maps old pre-1.23 request parameters Watchlist used to use (different from Recentchanges' ones)
194 * to the current ones.
196 * @param FormOptions $opts
197 * @return FormOptions
199 protected function fetchOptionsFromRequest( $opts ) {
200 static $compatibilityMap = [
201 'hideMinor' => 'hideminor',
202 'hideBots' => 'hidebots',
203 'hideAnons' => 'hideanons',
204 'hideLiu' => 'hideliu',
205 'hidePatrolled' => 'hidepatrolled',
206 'hideOwn' => 'hidemyself',
209 $params = $this->getRequest()->getValues();
210 foreach ( $compatibilityMap as $from => $to ) {
211 if ( isset( $params[$from] ) ) {
212 $params[$to] = $params[$from];
213 unset( $params[$from] );
217 if ( $this->getRequest()->getVal( 'action' ) == 'submit' ) {
218 $allBooleansFalse = [];
220 // If the user submitted the form, start with a baseline of "all
221 // booleans are false", then change the ones they checked. This
222 // means we ignore the defaults.
224 // This is how we handle the fact that HTML forms don't submit
226 foreach ( $this->filterGroups
as $filterGroup ) {
227 if ( $filterGroup instanceof ChangesListBooleanFilterGroup
) {
228 foreach ( $filterGroup->getFilters() as $filter ) {
229 $allBooleansFalse[$filter->getName()] = false;
234 $params = $params +
$allBooleansFalse;
237 // Not the prettiest way to achieve this… FormOptions internally depends on data sanitization
238 // methods defined on WebRequest and removing this dependency would cause some code duplication.
239 $request = new DerivativeRequest( $this->getRequest(), $params );
240 $opts->fetchValuesFromRequest( $request );
248 protected function buildQuery( &$tables, &$fields, &$conds, &$query_options,
249 &$join_conds, FormOptions
$opts ) {
251 $dbr = $this->getDB();
252 parent
::buildQuery( $tables, $fields, $conds, $query_options, $join_conds,
256 if ( $opts['days'] > 0 ) {
257 $conds[] = 'rc_timestamp > ' .
258 $dbr->addQuotes( $dbr->timestamp( time() - intval( $opts['days'] * 86400 ) ) );
265 protected function doMainQuery( $tables, $fields, $conds, $query_options,
266 $join_conds, FormOptions
$opts ) {
268 $dbr = $this->getDB();
269 $user = $this->getUser();
271 # Toggle watchlist content (all recent edits or just the latest)
272 if ( $opts['extended'] ) {
273 $limitWatchlist = $user->getIntOption( 'wllimit' );
276 # Top log Ids for a page are not stored
277 $nonRevisionTypes = [ RC_LOG
];
278 Hooks
::run( 'SpecialWatchlistGetNonRevisionTypes', [ &$nonRevisionTypes ] );
279 if ( $nonRevisionTypes ) {
280 $conds[] = $dbr->makeList(
282 'rc_this_oldid=page_latest',
283 'rc_type' => $nonRevisionTypes,
292 $tables = array_merge( [ 'recentchanges', 'watchlist' ], $tables );
293 $fields = array_merge( RecentChange
::selectFields(), $fields );
295 $query_options = array_merge( [ 'ORDER BY' => 'rc_timestamp DESC' ], $query_options );
296 $join_conds = array_merge(
301 'wl_user' => $user->getId(),
302 'wl_namespace=rc_namespace',
310 if ( $this->getConfig()->get( 'ShowUpdatedMarker' ) ) {
311 $fields[] = 'wl_notificationtimestamp';
313 if ( $limitWatchlist ) {
314 $query_options['LIMIT'] = $limitWatchlist;
317 $rollbacker = $user->isAllowed( 'rollback' );
318 if ( $usePage ||
$rollbacker ) {
320 $join_conds['page'] = [ 'LEFT JOIN', 'rc_cur_id=page_id' ];
322 $fields[] = 'page_latest';
326 // Log entries with DELETED_ACTION must not show up unless the user has
327 // the necessary rights.
328 if ( !$user->isAllowed( 'deletedhistory' ) ) {
329 $bitmask = LogPage
::DELETED_ACTION
;
330 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
331 $bitmask = LogPage
::DELETED_ACTION | LogPage
::DELETED_RESTRICTED
;
336 $conds[] = $dbr->makeList( [
337 'rc_type != ' . RC_LOG
,
338 $dbr->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask",
342 ChangeTags
::modifyDisplayQuery(
351 $this->runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds, $opts );
363 protected function runMainQueryHook( &$tables, &$fields, &$conds, &$query_options,
366 return parent
::runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds, $opts )
368 'SpecialWatchlistQuery',
369 [ &$conds, &$tables, &$join_conds, &$fields, $opts ],
375 * Return a IDatabase object for reading
379 protected function getDB() {
380 return wfGetDB( DB_REPLICA
, 'watchlist' );
386 public function outputFeedLinks() {
387 $user = $this->getUser();
388 $wlToken = $user->getTokenFromOption( 'watchlisttoken' );
390 $this->addFeedLinks( [
391 'action' => 'feedwatchlist',
393 'wlowner' => $user->getName(),
394 'wltoken' => $wlToken,
400 * Build and output the actual changes list.
402 * @param ResultWrapper $rows Database rows
403 * @param FormOptions $opts
405 public function outputChangesList( $rows, $opts ) {
406 $dbr = $this->getDB();
407 $user = $this->getUser();
408 $output = $this->getOutput();
410 # Show a message about replica DB lag, if applicable
411 $lag = wfGetLB()->safeGetLag( $dbr );
413 $output->showLagWarning( $lag );
416 # If no rows to display, show message before try to render the list
417 if ( $rows->numRows() == 0 ) {
418 $output->wrapWikiMsg(
419 "<div class='mw-changeslist-empty'>\n$1\n</div>", 'recentchanges-noresult'
424 $dbr->dataSeek( $rows, 0 );
426 $list = ChangesList
::newFromContext( $this->getContext(), $this->filterGroups
);
427 $list->setWatchlistDivs();
428 $list->initChangesListRows( $rows );
429 $dbr->dataSeek( $rows, 0 );
431 if ( $this->getConfig()->get( 'RCShowWatchingUsers' )
432 && $user->getOption( 'shownumberswatching' )
434 $watchedItemStore = MediaWikiServices
::getInstance()->getWatchedItemStore();
437 $s = $list->beginRecentChangesList();
438 $userShowHiddenCats = $this->getUser()->getBoolOption( 'showhiddencats' );
440 foreach ( $rows as $obj ) {
442 $rc = RecentChange
::newFromRow( $obj );
444 # Skip CatWatch entries for hidden cats based on user preference
446 $rc->getAttribute( 'rc_type' ) == RC_CATEGORIZE
&&
447 !$userShowHiddenCats &&
448 $rc->getParam( 'hidden-cat' )
453 $rc->counter
= $counter++
;
455 if ( $this->getConfig()->get( 'ShowUpdatedMarker' ) ) {
456 $updated = $obj->wl_notificationtimestamp
;
461 if ( isset( $watchedItemStore ) ) {
462 $rcTitleValue = new TitleValue( (int)$obj->rc_namespace
, $obj->rc_title
);
463 $rc->numberofWatchingusers
= $watchedItemStore->countWatchers( $rcTitleValue );
465 $rc->numberofWatchingusers
= 0;
468 $changeLine = $list->recentChangesLine( $rc, $updated, $counter );
469 if ( $changeLine !== false ) {
473 $s .= $list->endRecentChangesList();
475 $output->addHTML( $s );
479 * Set the text to be displayed above the changes
481 * @param FormOptions $opts
482 * @param int $numRows Number of rows in the result to show after this header
484 public function doHeader( $opts, $numRows ) {
485 $user = $this->getUser();
486 $out = $this->getOutput();
489 $this->msg( 'watchlistfor2', $user->getName() )
490 ->rawParams( SpecialEditWatchlist
::buildTools(
491 $this->getLanguage(),
492 $this->getLinkRenderer()
496 $this->setTopText( $opts );
498 $lang = $this->getLanguage();
499 if ( $opts['days'] > 0 ) {
500 $days = $opts['days'];
502 $days = $this->getConfig()->get( 'RCMaxAge' ) / ( 3600 * 24 );
504 $timestamp = wfTimestampNow();
505 $wlInfo = $this->msg( 'wlnote' )->numParams( $numRows, round( $days * 24 ) )->params(
506 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user )
507 )->parse() . "<br />\n";
509 $nondefaults = $opts->getChangedValues();
510 $cutofflinks = $this->msg( 'wlshowtime' ) . ' ' . $this->cutoffselector( $opts );
512 # Spit out some control panel links
514 $context = $this->getContext();
515 $namesOfDisplayedFilters = [];
516 foreach ( $this->getFilterGroups() as $groupName => $group ) {
517 if ( !$group->isPerGroupRequestParameter() ) {
518 foreach ( $group->getFilters() as $filterName => $filter ) {
519 if ( $filter->displaysOnUnstructuredUi( $this ) ) {
520 $namesOfDisplayedFilters[] = $filterName;
521 $links[] = $this->showHideCheck(
523 $filter->getShowHide(),
532 $hiddenFields = $nondefaults;
533 $hiddenFields['action'] = 'submit';
534 unset( $hiddenFields['namespace'] );
535 unset( $hiddenFields['invert'] );
536 unset( $hiddenFields['associated'] );
537 unset( $hiddenFields['days'] );
538 foreach ( $namesOfDisplayedFilters as $filterName ) {
539 unset( $hiddenFields[$filterName] );
545 # Namespace filter and put the whole form together.
547 $form .= $cutofflinks;
548 $form .= $this->msg( 'watchlist-hide' ) .
549 $this->msg( 'colon-separator' )->escaped() .
550 implode( ' ', $links );
551 $form .= "\n<br />\n";
552 $form .= Html
::namespaceSelector(
554 'selected' => $opts['namespace'],
556 'label' => $this->msg( 'namespace' )->text()
558 'name' => 'namespace',
560 'class' => 'namespaceselector',
563 $form .= '<span class="mw-input-with-label">' . Xml
::checkLabel(
564 $this->msg( 'invert' )->text(),
568 [ 'title' => $this->msg( 'tooltip-invert' )->text() ]
570 $form .= '<span class="mw-input-with-label">' . Xml
::checkLabel(
571 $this->msg( 'namespace_association' )->text(),
575 [ 'title' => $this->msg( 'tooltip-namespace_association' )->text() ]
577 $form .= Xml
::submitButton( $this->msg( 'watchlist-submit' )->text() ) . "\n";
578 foreach ( $hiddenFields as $key => $value ) {
579 $form .= Html
::hidden( $key, $value ) . "\n";
581 $form .= Xml
::closeElement( 'fieldset' ) . "\n";
582 $form .= Xml
::closeElement( 'form' ) . "\n";
583 $this->getOutput()->addHTML( $form );
585 $this->setBottomText( $opts );
588 function cutoffselector( $options ) {
589 // Cast everything to strings immediately, so that we know all of the values have the same
590 // precision, and can be compared with '==='. 2/24 has a few more decimal places than its
591 // default string representation, for example, and would confuse comparisons.
593 // Misleadingly, the 'days' option supports hours too.
594 $days = array_map( 'strval', [ 1/24, 2/24, 6/24, 12/24, 1, 3, 7 ] );
596 $userWatchlistOption = (string)$this->getUser()->getOption( 'watchlistdays' );
597 // add the user preference, if it isn't available already
598 if ( !in_array( $userWatchlistOption, $days ) && $userWatchlistOption !== '0' ) {
599 $days[] = $userWatchlistOption;
602 $maxDays = (string)( $this->getConfig()->get( 'RCMaxAge' ) / ( 3600 * 24 ) );
603 // add the maximum possible value, if it isn't available already
604 if ( !in_array( $maxDays, $days ) ) {
608 $selected = (string)$options['days'];
609 if ( $selected <= 0 ) {
610 $selected = $maxDays;
613 // add the currently selected value, if it isn't available already
614 if ( !in_array( $selected, $days ) ) {
618 $select = new XmlSelect( 'days', 'days', $selected );
621 foreach ( $days as $value ) {
623 $name = $this->msg( 'hours' )->numParams( $value * 24 )->text();
625 $name = $this->msg( 'days' )->numParams( $value )->text();
627 $select->addOption( $name, $value );
630 return $select->getHTML() . "\n<br />\n";
633 function setTopText( FormOptions
$opts ) {
634 $nondefaults = $opts->getChangedValues();
636 $user = $this->getUser();
638 $numItems = $this->countItems();
639 $showUpdatedMarker = $this->getConfig()->get( 'ShowUpdatedMarker' );
641 // Show watchlist header
643 if ( $numItems == 0 ) {
644 $form .= $this->msg( 'nowatchlist' )->parse() . "\n";
646 $form .= $this->msg( 'watchlist-details' )->numParams( $numItems )->parse() . "\n";
647 if ( $this->getConfig()->get( 'EnotifWatchlist' )
648 && $user->getOption( 'enotifwatchlistpages' )
650 $form .= $this->msg( 'wlheader-enotif' )->parse() . "\n";
652 if ( $showUpdatedMarker ) {
653 $form .= $this->msg( 'wlheader-showupdated' )->parse() . "\n";
658 if ( $numItems > 0 && $showUpdatedMarker ) {
659 $form .= Xml
::openElement( 'form', [ 'method' => 'post',
660 'action' => $this->getPageTitle()->getLocalURL(),
661 'id' => 'mw-watchlist-resetbutton' ] ) . "\n" .
662 Xml
::submitButton( $this->msg( 'enotif_reset' )->text(),
663 [ 'name' => 'mw-watchlist-reset-submit' ] ) . "\n" .
664 Html
::hidden( 'token', $user->getEditToken() ) . "\n" .
665 Html
::hidden( 'reset', 'all' ) . "\n";
666 foreach ( $nondefaults as $key => $value ) {
667 $form .= Html
::hidden( $key, $value ) . "\n";
669 $form .= Xml
::closeElement( 'form' ) . "\n";
672 $form .= Xml
::openElement( 'form', [
674 'action' => wfScript(),
675 'id' => 'mw-watchlist-form'
677 $form .= Html
::hidden( 'title', $this->getPageTitle()->getPrefixedText() );
678 $form .= Xml
::fieldset(
679 $this->msg( 'watchlist-options' )->text(),
681 [ 'id' => 'mw-watchlist-options' ]
684 $form .= $this->makeLegend();
686 $this->getOutput()->addHTML( $form );
689 protected function showHideCheck( $options, $message, $name, $value ) {
690 $options[$name] = 1 - (int)$value;
692 return '<span class="mw-input-with-label">' . Xml
::checkLabel(
693 $this->msg( $message, '' )->text(),
701 * Count the number of paired items on a user's watchlist.
702 * The assumption made here is that when a subject page is watched a talk page is also watched.
703 * Hence the number of individual items is halved.
707 protected function countItems() {
708 $store = MediaWikiServices
::getInstance()->getWatchedItemStore();
709 $count = $store->countWatchedItems( $this->getUser() );
710 return floor( $count / 2 );